<template>
  <div class="resource-card" :class="getCardClass()">
    <div class="resource-header">
      <div class="resource-icon">{{ resource.icon }}</div>
      <div class="resource-info">
        <div class="resource-name">{{ resource.name }}</div>
        <div class="resource-amount">{{ formatAmount(resource.amount) }}</div>
      </div>
      <div class="resource-actions">
        <button 
          class="action-btn"
          @click="$emit('trade', resource.id)"
          title="交易"
        >
          💱
        </button>
      </div>
    </div>
    
    <div class="resource-stats">
      <div class="stat-item">
        <span class="stat-label">生产:</span>
        <span class="stat-value" :class="getProductionClass()">
          {{ formatProduction(resource.production) }}
        </span>
      </div>
      
      <div class="stat-item">
        <span class="stat-label">存储:</span>
        <span class="stat-value">
          {{ formatAmount(resource.storage) }}
        </span>
      </div>
    </div>
    
    <div class="resource-progress">
      <div class="progress-bar">
        <div 
          class="progress-fill" 
          :style="{ width: `${resource.utilization}%` }"
          :class="getUtilizationClass()"
        ></div>
      </div>
      <div class="progress-text">
        {{ Math.round(resource.utilization) }}% 已使用
      </div>
    </div>
    
    <div class="resource-footer">
      <button 
        class="upgrade-btn"
        @click="$emit('upgrade-storage', resource.id)"
        :disabled="!canUpgrade"
      >
        <span class="btn-icon">⬆️</span>
        升级存储
      </button>
      
      <div class="resource-trend" :class="getTrendClass()">
        <span class="trend-icon">{{ getTrendIcon() }}</span>
        <span class="trend-text">{{ getTrendText() }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface ResourceData {
  id: string
  name: string
  icon: string
  amount: number
  production: number
  storage: number
  utilization: number
}

interface Props {
  resource: ResourceData
}

interface Emits {
  (e: 'upgrade-storage', resourceId: string): void
  (e: 'trade', resourceId: string): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const canUpgrade = computed(() => {
  // 简单的升级条件检查
  return props.resource.utilization > 50
})

const timeToFull = computed(() => {
  if (props.resource.production <= 0) return Infinity
  const remaining = props.resource.storage - props.resource.amount
  return remaining / (props.resource.production / 60) // 转换为秒
})

function formatAmount(amount: number): string {
  if (amount >= 1000000000) {
    return (amount / 1000000000).toFixed(1) + 'B'
  } else if (amount >= 1000000) {
    return (amount / 1000000).toFixed(1) + 'M'
  } else if (amount >= 1000) {
    return (amount / 1000).toFixed(1) + 'K'
  } else {
    return Math.floor(amount).toString()
  }
}

function formatProduction(production: number): string {
  const sign = production >= 0 ? '+' : ''
  return `${sign}${formatAmount(production)}/分`
}

function getCardClass(): string {
  if (props.resource.utilization >= 90) return 'critical'
  if (props.resource.utilization >= 75) return 'warning'
  if (props.resource.production < 0) return 'deficit'
  return 'normal'
}

function getProductionClass(): string {
  if (props.resource.production > 0) return 'positive'
  if (props.resource.production < 0) return 'negative'
  return 'neutral'
}

function getUtilizationClass(): string {
  if (props.resource.utilization >= 90) return 'critical'
  if (props.resource.utilization >= 75) return 'warning'
  return 'normal'
}

function getTrendClass(): string {
  if (props.resource.production > 0) return 'trend-up'
  if (props.resource.production < 0) return 'trend-down'
  return 'trend-stable'
}

function getTrendIcon(): string {
  if (props.resource.production > 0) return '📈'
  if (props.resource.production < 0) return '📉'
  return '➡️'
}

function getTrendText(): string {
  if (props.resource.production > 0) {
    if (timeToFull.value < 3600) { // 小于1小时
      const minutes = Math.floor(timeToFull.value / 60)
      return `${minutes}分钟后满仓`
    } else if (timeToFull.value < 86400) { // 小于1天
      const hours = Math.floor(timeToFull.value / 3600)
      return `${hours}小时后满仓`
    } else {
      return '增长中'
    }
  } else if (props.resource.production < 0) {
    const timeToEmpty = props.resource.amount / Math.abs(props.resource.production / 60)
    if (timeToEmpty < 3600) {
      const minutes = Math.floor(timeToEmpty / 60)
      return `${minutes}分钟后耗尽`
    } else if (timeToEmpty < 86400) {
      const hours = Math.floor(timeToEmpty / 3600)
      return `${hours}小时后耗尽`
    } else {
      return '消耗中'
    }
  } else {
    return '稳定'
  }
}
</script>

<style scoped>
.resource-card {
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 1rem;
  backdrop-filter: blur(5px);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.resource-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--accent-color);
  opacity: 0.8;
}

.resource-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent-color);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.resource-card.normal {
  --accent-color: #00d4ff;
}

.resource-card.warning {
  --accent-color: #f59e0b;
}

.resource-card.critical {
  --accent-color: #ef4444;
}

.resource-card.deficit {
  --accent-color: #8b5cf6;
}

.resource-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.resource-icon {
  font-size: 2rem;
  min-width: 40px;
  text-align: center;
}

.resource-info {
  flex: 1;
}

.resource-name {
  font-size: 1rem;
  font-weight: 600;
  color: #ffffff;
  margin-bottom: 0.25rem;
}

.resource-amount {
  font-size: 1.5rem;
  font-weight: bold;
  color: var(--accent-color);
}

.resource-actions {
  display: flex;
  gap: 0.25rem;
}

.action-btn {
  width: 32px;
  height: 32px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.resource-stats {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
}

.stat-label {
  font-size: 0.75rem;
  color: #888888;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #ffffff;
}

.stat-value.positive {
  color: #00ff88;
}

.stat-value.negative {
  color: #ff4444;
}

.stat-value.neutral {
  color: #888888;
}

.resource-progress {
  margin-bottom: 1rem;
}

.progress-bar {
  height: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}

.progress-fill {
  height: 100%;
  transition: width 0.3s ease;
}

.progress-fill.normal {
  background: linear-gradient(90deg, #00d4ff, #0099cc);
}

.progress-fill.warning {
  background: linear-gradient(90deg, #f59e0b, #d97706);
}

.progress-fill.critical {
  background: linear-gradient(90deg, #ef4444, #dc2626);
}

.progress-text {
  font-size: 0.75rem;
  color: #cccccc;
  text-align: center;
}

.resource-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.upgrade-btn {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.75rem;
  background: rgba(0, 212, 255, 0.2);
  color: #00d4ff;
  border: 1px solid #00d4ff;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.upgrade-btn:hover:not(:disabled) {
  background: #00d4ff;
  color: #000000;
  transform: translateY(-1px);
}

.upgrade-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 0.875rem;
}

.resource-trend {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
}

.trend-up {
  color: #00ff88;
}

.trend-down {
  color: #ff4444;
}

.trend-stable {
  color: #888888;
}

.trend-icon {
  font-size: 0.875rem;
}

.trend-text {
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .resource-card {
    padding: 0.75rem;
  }
  
  .resource-header {
    gap: 0.5rem;
  }
  
  .resource-icon {
    font-size: 1.5rem;
    min-width: 30px;
  }
  
  .resource-amount {
    font-size: 1.25rem;
  }
  
  .resource-footer {
    flex-direction: column;
    gap: 0.5rem;
    align-items: stretch;
  }
  
  .upgrade-btn {
    justify-content: center;
  }
  
  .resource-trend {
    justify-content: center;
  }
}
</style>